package com.kyist.frp.controller;

import com.kyist.frp.config.FrpConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Objects;

/**
 * 登录
 * @Author kyist
 * @Date 2021/12/15 21:51
 */
@Controller
public class LoginController {

    @RequestMapping(value = {"/","/login/getLogin"})
    public String getLogin(ModelMap map){
        return "login";
    }

    @RequestMapping("/login/login")
    @ResponseBody
    public ResponseEntity<String> login(@RequestBody Map<String,Object> map, HttpServletRequest request){
        Object username = map.get("username");
        Object password = map.get("password");
        //获取配置文件账号和密码
        Object u1=null;
        Object p1=null;
        if (FrpConfig.prop != null) {
            u1 = FrpConfig.prop.get("username");
            p1 = FrpConfig.prop.get("password");
        }
        u1 = u1==null?"admin":u1;
        p1 = p1==null?"123456":p1;
        String msg = "登录失败，账号或密码错误";
        if (Objects.equals(username,u1) && Objects.equals(password,p1) ){
            HttpSession session = request.getSession();
            session.setAttribute("USER",username);
            msg = "success";
        }
        return new ResponseEntity<>(msg, HttpStatus.OK);
    }

    @RequestMapping("/jump/html")
    public String jump(String html){

        return html;
    }



}
